public class Test {
  public static String picA = 
    "    o    \n" +
    "   ooo   \n" +
    "  ooooo  \n" +
    "   ooo   \n" +
    "    o    ";

  public static String picB = 
    "  oo oo  \n" +
    "  o   o  \n" +
    "  o   o  \n" +
    "  o   o  \n" +
    "  oo oo  ";

  public static void drawPicture(String picture) {
    // Move cursor up 4 lines.
    // "((char) 27)" converts the number 27 to a character, which is
    // a strange crazy escape character that makes the terminal do
    // funky stuff. See http://ascii-table.com/ansi-escape-sequences-vt-100.php
    System.out.print(((char) 27));// + "[4A");

    // Move cursor to the start of the current line.
    System.out.print("\r");
  
    // Draw the picture.
    System.out.print(picture);
    System.out.flush(); // Need to call this since there's no newline at the end!
  }

  public static void main(String[] args) {
    // Print out the initial picture. Can't call drawPicture as that would
    // attempt to move up 4 lines straight away!
    System.out.print(picA);
    System.out.flush();

    for (int i = 0; i < 25; i++) {
      // Alternate between picA and picB
      if (i % 2 == 0) {
        drawPicture(picB);
      } else {
        drawPicture(picA);
      }

      // Sleep for 0.3 seconds.
      try {
        Thread.sleep(300);
      } catch (InterruptedException ie) {
        // Do nothing! This exception is required for all Thread methods,
        // and is rarely thrown. It just covers the case where other code
        // interrupts the sleeping.
      }
    }

    // Make sure we print out the final newline once finished.
    System.out.println();
  }
}
